/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modele.servlet;

import Modele.TD;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.ParserConfigurationException;
import org.jdom.JDOMException;
import org.xml.sax.SAXException;

/**
 *
 * @author DAVID
 */
@WebServlet(name = "creationSujet", urlPatterns = {"/creationSujet"})
public class creationSujet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {



    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("application/xml");
        PrintWriter out = response.getWriter();
        
        HttpSession session = request.getSession(true);
        if (request.getParameter("metier").equals("creationTD")) {
            String titreTD = request.getParameter("titreTD");
            String date = request.getParameter("date");
            String nom = request.getParameter("nom");
            String prenom = request.getParameter("prenom");
            String contenu = request.getParameter("contenu");
            String titreExo = request.getParameter("titreExo");

            request.setAttribute("test", request.getParameter("titreTD"));

            RequestDispatcher dispatcher = request.getRequestDispatcher("sujet.jsp");
            dispatcher.forward(request, response);
            
            
            
            String strXml = "<sujet> "
                    + "<titre> <%= request.getAttribute(\"test\") %> </titre>"
                    + "<auteur nom =\"<%= request.getParameter(\"nom\") %>\"  prenom =\" <%= request.getParameter(\"prenom\") %>\"/>"
                    + "<date> <%= request.getParameter(\"date\") %> </date>"
                    + "<exercice titre=\"<%= request.getParameter(\"titreExo\") %>\">"
                    + "<contenu> <%= request.getParameter(\"contenu\") %></contenu>"
                    + "</exercice>"
                    +"</sujet>";
            
            
        } else if (request.getParameter("metier").equals("creationinstTD"))
             {
            try {
                String xml = request.getParameter("str_xml");
                TD monTd = new TD(xml);

            } catch (ParserConfigurationException ex) {
                Logger.getLogger(creationSujet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SAXException ex) {
                Logger.getLogger(creationSujet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (JDOMException ex) {
                Logger.getLogger(creationSujet.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}
